package baoxiu.controller;

import baoxiu.dto.BuildDetailTotal;
import baoxiu.dto.BuildTotal;
import baoxiu.dto.GoodsTotal;
import baoxiu.dto.RepaireDTO;
import baoxiu.entity.Building;
import baoxiu.entity.BulidingDetail;
import baoxiu.entity.Goods;
import baoxiu.entity.Repaire;
import baoxiu.service.BuildingDetailService;
import baoxiu.service.BuildingService;
import baoxiu.service.GoodsService;
import baoxiu.service.RepaireService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import common.exception.AppException;
import common.util.Res;
import common.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName RepaireController
 * @Author mengjiafeng
 * @Date 2019/7/24 20:47
 **/
@RestController
@RequestMapping("/baoxiu/repaire")
public class RepaireController {
    @Autowired
    private RepaireService repaireService;
    private BuildingDetailService buildingDetailService;
    private BuildingService buildingService;
    private GoodsService goodsService;

    /**
     * 查询所有报修记录
     * @param page
     * @return
     */
    @GetMapping("/getAllRepaire")
    public Result getAllRepaire(Repaire repaire,Page page){
        return Res.makeOKRsp(repaireService.page(page,new QueryWrapper<Repaire>().like("user_name",repaire.getUserName())));
    }
    @GetMapping("/getAllrepaireDetailById")
    public Result getAllrepaireDetailById(String rid){
        Repaire repaire = repaireService.getById(rid);
        return Res.makeOKRsp(repaire);
    }

    /**
     * 删除报修记录
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(String id) {
        try {
            repaireService.removeById(id);
            return Res.makeOKRsp("删除成功");
        } catch (AppException e) {
            return Res.makeErrRsp("删除失败");
        }
    }
    /**
     * 添加报修记录
     * @param repaire
     * @return
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody Repaire repaire){
        BulidingDetail bulidingDetail = buildingDetailService.getById(repaire.getBuildingDetailId());
        repaire.setBuildingDetailName(bulidingDetail.getBuildName());
        Building building = buildingService.getById(repaire.getBuildingId());
        repaire.setBuildingName(building.getName());
        Goods goods = goodsService.getById(repaire.getGoodsId());
        repaire.setGoodsName(goods.getName());
        repaire.setRepaireStatus("0");
        repaireService.save(repaire);
        return Res.makeOKRsp();
    }

    /**
     * 通过用户id获取全部维修记录
     * @param userId
     * @return
     */
    @GetMapping("/getAllRepaireByUid")
    public Result<List<RepaireDTO>> getAllRepaireById(String userId) {
        if (StringUtils.isNotBlank(userId)) {
            List<RepaireDTO> list = repaireService.getAllRepaireByUId(userId);
            return Res.makeOKRsp(list);
        }
        return Res.makeErrRsp("");
    }

    /**
     * 通过维修员id获取全部维修记录
     * @param repairemanId
     * @return
     */
    @GetMapping("/getAllRepaireByMid")
    public Result<List<Repaire>> getAllRepaireByMId(String repairemanId) {
        if (StringUtils.isNotBlank(repairemanId)) {
            List<Repaire> list = repaireService.list(new QueryWrapper<Repaire>().eq("repaireman_id",repairemanId));
            return Res.makeOKRsp(list);
        }
        return Res.makeErrRsp("");
    }

    /**
     * 获取全部未维修记录
     *
     * @param
     */
    @GetMapping("/getAllRepaireByRepaireStatus")
    @ResponseBody
    public Result<List<Repaire>> getAllRepaireByRepaireStatus(){
        List<Repaire> repaireList = repaireService.getAllRepaireByRepaireStatus();
        return Res.makeOKRsp(repaireList);
    }

    /**
     * 维修员接单
     *
     * @param
     */
    @PutMapping("/toRepaire")
    public Result toRepaire(String repairemanId,String rid) {
        repaireService.toRepaire(repairemanId,rid);
        return Res.makeOKRsp("接单成功");
    }

    /**
     * 维修员完成订单
     * @param
     * @return
     */
    @GetMapping("/overOrder")
    public Result getOrder(String rid){
        repaireService.update(new UpdateWrapper<Repaire>().set("repaire_status","2").eq("rid",rid));
         return Res.makeOKRsp("维修员确认完成订单");
    }


    /**
     * 用户确认完成订单
     */
    @GetMapping("/finishOrder")
    public Result finishOrder(String rid){
        repaireService.update(new UpdateWrapper<Repaire>().set("confirm_status", "1").set("over_time",new Date()).eq("rid",rid));
        return Res.makeOKRsp("用户确认完成订单");
    }

    /**
     * 添加评论
     * @param rid
     * @param evaluate
     * @return
     */
    @PostMapping("/addEvaluate")
    public Result addEvaluate(String rid,String evaluate){
        repaireService.update(new UpdateWrapper<Repaire>().set("evaluate",evaluate).eq("rid",rid));
        return Res.makeOKRsp("添加评论成功");
    }

    /**
     * 报修物品统计
     * @return
     */
    @GetMapping("/getGoodsTotal")
    public Result<List<GoodsTotal>> getGoodsTotal(){
        List<GoodsTotal> list = repaireService.getGoodsTotal();
        return Res.makeOKRsp(list);
    }

    /**
     * 教学楼和公寓楼报修总数统计
     * @return
     */
    @GetMapping("/getBuildTotal")
    public Result<List<BuildTotal>> getBuildTotal(){
        List<BuildTotal> list = repaireService.getBuildTotal();
        return Res.makeOKRsp(list);
    }

    /**
     * 具体地点报修统计
     * @return
     */
    @GetMapping("/getBuildDetailTotal")
    public Result<List<BuildDetailTotal>> getBuildDetailTotal(){
        List<BuildDetailTotal> detailTotalList = repaireService.getBuildDetailTotal();
        return Res.makeOKRsp(detailTotalList);
    }
}
